home *** CD-ROM | disk | FTP | other *** search
/ Mac OS 9 Serial Number Archive / SN Archive 2023.11.04.toast / BSNG / SDK / BSNG SDK 2.6 / Contributed / BSNG Product DeRez / SillyBalls.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-22  |  7.4 KB  |  404 lines  |  [TEXT/CWIE]

  1. #include <assert.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4.  
  5. #if PRAGMA_ALIGN_SUPPORTED
  6. #pragma options align=mac68k
  7. #endif
  8.  
  9. typedef struct CIconHeader
  10. {
  11.     PixMap    iconPMap;
  12.     BitMap    iconMask;
  13.     BitMap    iconBMap;
  14.     Handle    iconData;
  15. } CIconHeader;
  16.  
  17. typedef struct ColorTable256
  18. {
  19.     long           ctSeed;
  20.     short       ctFlags;
  21.     short         ctSize;
  22.     ColorSpec ctTable[256];
  23. } ColorTable256;
  24.  
  25. typedef struct CIconResource
  26. {
  27.     CIconHeader   cicn;
  28.     UInt32        theMask[32];
  29.     UInt32        theBitMap[32];
  30.     ColorTable256 theCTable;
  31.     UInt8         thePixels[32 * 32];
  32. } CIconResource;
  33.  
  34. #if PRAGMA_ALIGN_SUPPORTED
  35. #pragma options align=reset
  36. #endif
  37.  
  38. void GetVersion(char* outString);
  39. void GenerateHeader(char* inName, char* inVersion);
  40. void GenerateRez(CIconResource* inCicn);
  41. CIconResource* MakeCICN(FSSpecPtr inSpec);
  42.  
  43. CIconResource* CreateCICN(void);
  44. void FillCICNClut(CIconResource* ioCicn);
  45. OSType GetFileType(FSSpecPtr inSpec);
  46. short FindIconID(OSType inFileType);
  47. void FillCICNBitMap(CIconResource* ioCICN, short inResID);
  48. void FillCICNPixMap(CIconResource* ioCICN, short inResID);
  49.  
  50. int main(void)
  51. {
  52.     short          resRef, oldResRef;
  53.     CIconResource* cicn;
  54.     char           name[256];
  55.     char           version[256];
  56.     FSSpec         spec;
  57.  
  58.     oldResRef = CurResFile();
  59.  
  60.     FSMakeFSSpec(0, 0, "\pRestricted:Numbers:BSNG 2.5:BSNG 2.5 (PPC)", &spec);
  61.     
  62.     // Get filename.
  63.     BlockMoveData(spec.name + 1, name, spec.name[0]);
  64.     name[spec.name[0]] = '\0';
  65.  
  66.     resRef = FSpOpenResFile(&spec, fsRdPerm);
  67.     assert(resRef);
  68.     
  69.     UseResFile(resRef);
  70.     
  71.     // Get version data.
  72.     GetVersion(version);
  73.     
  74.     // Get icon data.
  75.   cicn = MakeCICN(&spec);
  76.   
  77.     UseResFile(oldResRef);    
  78.     CloseResFile(resRef);
  79.  
  80.     // Now spit out data.
  81.     GenerateHeader(name, version);
  82.     GenerateRez(cicn);
  83.     
  84.     DisposePtr((Ptr) cicn);
  85.     
  86.     return 0;
  87. }
  88.  
  89.  
  90.  
  91. void GenerateHeader(char* inName, char* inVersion)
  92. {
  93.   char  outString[32];
  94.   FILE* fp;
  95.   
  96.     assert(inName && inVersion);
  97.     
  98.     // If the name + version is too long,
  99.     if (strlen(inName) + strlen(inVersion + 1) > 27)
  100.     {
  101.         // And the name by itself is too long,
  102.         if (strlen(inName) > 27)
  103.         {
  104.             // Truncate the name and trail it with ...
  105.             strncpy(outString, inName, 27);
  106.             outString[26] = '…';
  107.             outString[27] = '\0';
  108.         }
  109.         else
  110.         {
  111.             // Use only the name if it fits.
  112.             strcpy(outString, inName);
  113.         }
  114.     }
  115.     else
  116.     {
  117.         // Use name + version if it fits.
  118.         strcpy(outString, inName);
  119.         strcat(outString, " ");
  120.         strcat(outString, inVersion);
  121.     }
  122.  
  123.     // Create the .h file.
  124.     fp = fopen("BSNG Product.h", "w");
  125.  
  126.   fprintf(fp, "/* BSNG Product.h */\r");
  127.   fprintf(fp, "/* This file is generated by BSNG Product DeRez once per product.  */\r");
  128.   fprintf(fp, "/* Feel free to alter the product description, up to 27 chars. max */\r\r");
  129.     fprintf(fp, "#define PROGRAM_NAME \"%s\"\r\r", outString);
  130.     
  131.     fclose(fp);
  132. }
  133.  
  134.  
  135. void GenerateRez(CIconResource* inCicn)
  136. {
  137.     FILE*  fp;
  138.     long   i, length;
  139.     UInt8* cicnP;
  140.     
  141.     assert(inCicn);
  142.  
  143.     fp = fopen("BSNG Product.r", "w");
  144.  
  145.   fprintf(fp, "/* BSNG Product.r */\r");
  146.   fprintf(fp, "/* This file is generated by BSNG Product DeRez once per product. */\r\r");
  147.   fprintf(fp, "#include \"BSNG Product.h\"\r\r");
  148.   fprintf(fp, "data 'cicn' (1000) {\r");
  149.     
  150.     length = sizeof(CIconResource);
  151.     cicnP = (UInt8*) inCicn;
  152.     for (i = 0; i < length; i++)
  153.     {
  154.         if ((i % 16) == 0)
  155.         {
  156.             fprintf(fp, "    $\"");
  157.         }
  158.         else if ((i % 2) == 0)
  159.         {
  160.             fprintf(fp, " ");
  161.         }
  162.         
  163.         fprintf(fp, "%02X", (int) cicnP[i]);
  164.  
  165.         if ((i % 16) == 15)
  166.         {
  167.             fprintf(fp, "\"\r");
  168.         }
  169.     }
  170.     
  171.     if ((i % 16) != 0)
  172.     {
  173.         fprintf(fp, "\"\r");
  174.     }
  175.     
  176.     fprintf(fp, "};\r\r");
  177.     
  178.     fclose(fp);
  179. }
  180.  
  181.  
  182.  
  183. // spit out BSNG Product.r
  184.  
  185. // get file name
  186. // get 'vers' info
  187. // spit out BSNG Product.h
  188.  
  189. void GetVersion(char* outString)
  190. {
  191.     Handle    versH;
  192.     StringPtr str;
  193.     
  194.     versH = Get1Resource('vers', 2);
  195.     if (versH == NULL)
  196.         versH = Get1Resource('vers', 1);
  197.     assert(versH);
  198.  
  199.     HLock(versH);
  200.  
  201.     str = (UInt8*) (*versH + 6);
  202.     BlockMoveData(str + 1, outString, str[0]);
  203.     outString[str[0]] = '\0';
  204.  
  205.     HUnlock(versH);
  206.     ReleaseResource(versH);
  207. }
  208.  
  209.  
  210.  
  211. CIconResource* MakeCICN(FSSpecPtr inSpec)
  212. {
  213.     CIconResource* cicn;
  214.     short          id;
  215.     
  216.     cicn = CreateCICN();
  217.     assert(cicn);
  218.     
  219.     FillCICNClut(cicn);
  220.     
  221.     id = FindIconID(GetFileType(inSpec));
  222.     FillCICNPixMap(cicn, id);
  223.     FillCICNBitMap(cicn, id);
  224.  
  225.     return cicn;
  226. }
  227.  
  228.  
  229.  
  230. OSType GetFileType(FSSpecPtr inSpec)
  231. {
  232.     FInfo finderInfo;
  233.     
  234.     FSpGetFInfo(inSpec, &finderInfo);
  235.     return finderInfo.fdType;
  236. }
  237.  
  238.  
  239. short FindIconID(OSType inFileType)
  240. {
  241.     Str255 name;
  242.     OSType type;
  243.     short  outID = -1;
  244.     long   i, count;
  245.     Handle frefH;
  246.     
  247.     // Iterate FREF resources until we find one whose type (1st 4 bytes) == the file type.
  248.     count = Count1Resources('FREF');
  249.     for (i = 1; i <= count; i++)
  250.     {
  251.         frefH = Get1IndResource('FREF', i);
  252.         assert(frefH);
  253.         if (**(OSType**) frefH == inFileType)
  254.         {
  255.             GetResInfo(frefH, &outID, &type, name);
  256.         }
  257.         ReleaseResource(frefH);
  258.     }
  259.     
  260.     return outID;
  261. }
  262.  
  263. void FillCICNClut(CIconResource* ioCicn)
  264. {
  265.     CTabHandle  clutH;
  266.     ColorTable* clutP;
  267.     long        i;
  268.     
  269.     assert(ioCicn);
  270.     
  271.     // CLUT 8 is the standard 8-bit color system color table and is always present
  272.     clutH = GetCTable(8);
  273.     assert(clutH);
  274.  
  275.     // Copy the data into the cicn's color table.
  276.     HLock((Handle) clutH);
  277.     clutP = *clutH;
  278.     
  279.     ioCicn->theCTable.ctSeed  = 0;
  280.     ioCicn->theCTable.ctFlags = 0;
  281.     ioCicn->theCTable.ctSize  = clutP->ctSize;
  282.     for (i = 0; i <= clutP->ctSize; i++)
  283.     {
  284.       ioCicn->theCTable.ctTable[i].value = i;
  285.         ioCicn->theCTable.ctTable[i].rgb   = clutP->ctTable[i].rgb;
  286.     }
  287.     
  288.     HUnlock((Handle) clutH);
  289. }
  290.  
  291.  
  292. void FillCICNPixMap(CIconResource* ioCICN, short inResID)
  293. {
  294.   UInt8* icl8P;
  295.     Handle icl8H;
  296.     long   i;
  297.     
  298.     assert(ioCICN);
  299.   
  300.     // Load the 'icl8' resource specified by inResID and paste it into this cicn's
  301.     // pixmap data.
  302.     
  303.     icl8H = Get1Resource('icl8', inResID);
  304.     assert(icl8H);
  305.     assert(GetHandleSize(icl8H) == 1024);
  306.     
  307.     HLock(icl8H);
  308.     icl8P = (UInt8*) *icl8H;
  309.     for (i = 0; i < 1024; i++)
  310.     {
  311.         ioCICN->thePixels[i] = icl8P[i];
  312.     }
  313.     
  314.     HUnlock(icl8H);
  315.     ReleaseResource(icl8H);
  316. }
  317.  
  318.  
  319.  
  320. void FillCICNBitMap(CIconResource* ioCICN, short inResID)
  321. {
  322.   UInt32* iconP;
  323.   Handle  iconH;
  324.   long    i;
  325.  
  326.     assert(ioCICN);
  327.   
  328.     // Load the ICN# resource specified by inResID and paste it into this cicn's
  329.     // pixmap data.
  330.     
  331.     iconH = Get1Resource('ICN#', inResID);
  332.     assert(iconH);
  333.     assert(GetHandleSize(iconH) == 256);
  334.     
  335.     HLock(iconH);
  336.  
  337.     iconP = (UInt32*) *iconH;
  338.     for (i = 0; i < 32; i++)
  339.     {
  340.         ioCICN->theBitMap[i] = iconP[i];
  341.     }
  342.     
  343.     iconP += 32;
  344.     for (i = 0; i < 32; i++)
  345.     {
  346.         ioCICN->theMask[i] = iconP[i];
  347.     }    
  348.     
  349.     HUnlock(iconH);
  350.     ReleaseResource(iconH);
  351. }
  352.  
  353.  
  354.  
  355. CIconResource* CreateCICN(void)
  356. {
  357.     Rect           theRect;
  358.     int            theImageRowBytes, theBitsPixel;
  359.     CIconResource* outCICN;
  360.     PixMap*        pm;
  361.     BitMap*        bm;
  362.     
  363.     SetRect(&theRect, 0, 0, 32, 32);
  364.     
  365.     // Use an 8-bit pixel map.
  366.     theBitsPixel     = 8;
  367.     theImageRowBytes = 0x8020;
  368.     
  369.     outCICN = (CIconResource*) NewPtr(sizeof(CIconResource));
  370.     assert(outCICN);
  371.     
  372.     // Initialize the headers.
  373.     pm = &outCICN->cicn.iconPMap;
  374.     pm->baseAddr   = 0;
  375.     pm->rowBytes   = theImageRowBytes;
  376.     pm->bounds     = theRect;
  377.     pm->pmVersion  = 0;
  378.     pm->packType   = 0;
  379.     pm->packSize   = 0;
  380.     pm->hRes       = 0x00480000; // 72 dpi in Fixed format
  381.     pm->vRes       = 0x00480000;
  382.     pm->pixelType  = 0;
  383.     pm->pixelSize  = theBitsPixel;
  384.     pm->cmpCount   = 1;
  385.     pm->cmpSize    = theBitsPixel;
  386.     pm->planeBytes = 0;
  387.     pm->pmTable    = 0;
  388.     pm->pmReserved = 0;
  389.  
  390.   bm = &outCICN->cicn.iconMask;
  391.   bm->baseAddr   = 0;
  392.   bm->rowBytes   = 4;
  393.   bm->bounds     = theRect;
  394.  
  395.   bm = &outCICN->cicn.iconBMap;
  396.   bm->baseAddr   = 0;
  397.   bm->rowBytes   = 4;
  398.   bm->bounds     = theRect;
  399.  
  400.   outCICN->cicn.iconData = 0;
  401.  
  402.     return outCICN;
  403. }
  404.